package com.allwees.bs.c.common.auth.oauth.google;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.allwees.bs.c.common.auth.constant.EAuthType;
import com.allwees.bs.c.module.user.req.ThirdPartLoginReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * Mock认证处理器
 *
 */
@Component
@Lazy(false)
public class MockGoogleAuthHandler extends BaseGoogleAuthHandler {
	@Override
	public EAuthType getAuthType() {
		return EAuthType.MOCK_GOOGLE;
	}

	@Override
	protected JSONObject fetchUserInfo(ThirdPartLoginReq req) {
		String firstName = RandomUtil.randomString(3);
		String lastName = RandomUtil.randomNumbers(4);
		JSONObject userInfo = new JSONObject();
		//Mock模式下，利用token字段模拟id
		userInfo.put("id", StringUtils.isNotBlank(req.getToken()) ? req.getToken() : "mock_" + RandomUtil.randomNumbers(8));
		if(req.isHasEmail()) {
			userInfo.put("email", StringUtils.isNotBlank(req.getMockEmail()) ? req.getMockEmail() : firstName + "." + lastName + "@mock.com");
		} else {
			userInfo.put("email", null);
		}
		userInfo.put("given_name", firstName);
		userInfo.put("family_name", lastName);
		userInfo.put("picture", "mock.gif");
		return userInfo;
	}
}
